const express = require('express');
const router = express.Router();
const db = require('../config/database');

// 获取尺寸列表
router.get('/', async (req, res) => {
  try {
    const query = `
      SELECT 
        s.id,
        s.name,
        s.width,
        s.height,
        s.category,
        s.is_default,
        s.created_at,
        s.updated_at
      FROM sizes s
      ORDER BY s.category ASC, s.is_default DESC, s.created_at DESC
    `;
    
    const [rows] = await db.execute(query);
    
    // 格式化数据
    const data = rows.map(row => ({
      id: row.id,
      attributes: {
        name: row.name,
        width: row.width,
        height: row.height,
        category: row.category,
        is_default: row.is_default,
        createdAt: row.created_at,
        updatedAt: row.updated_at
      }
    }));
    
    res.json({ data });
  } catch (error) {
    console.error('获取尺寸列表失败:', error);
    res.status(500).json({ error: '获取尺寸列表失败' });
  }
});

module.exports = router;